package com.xrh.xdemo.service.controllers;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;

import com.xrh.xdemo.api.dto.entity.MarkLibDTO;
import com.xrh.xdemo.api.request.addRequest.MarkLibAddRequest;
import com.xrh.xdemo.api.request.queryRequest.MarkLibQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.MarkLibUpdateRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.service.MarkLibService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="标签库API")
@RestController
@RequestMapping("markLib")
@Slf4j
public class MarkLibController {

    @Resource
    private MarkLibService markLibService;

    
    /**
   	 * @function 新增标签库
   	 * @param markLibAddRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-25 14:58:40
   	*/
   	@ApiOperation("新增标签库")
   	@PostMapping("/addMarkLib")
   	@OperateTrace(operateType = "新增" , businessType = "标签库" , businessNoKeyPath = "data" , businessNoFromReturnResult = true ,  titleKeyPath = "libName" )
   	public  CommonResult<Long> addMarkLib(@Valid @RequestBody MarkLibAddRequest markLibAddRequest){
   		
   		try {
   		
   			Long id = markLibService.addMarkLib(markLibAddRequest);
   			
   			return CommonResult.success(id);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("addMarkLib 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 删除标签库
   	 * @author 肖荣辉
   	 * @date 2023-12-25 14:58:40
   	*/
   	@ApiOperation("删除标签库")
   	@ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/deleteMarkLibById")
   	@OperateTrace(operateType = "删除" , businessType = "标签库" , businessNoKeyPath = "id")
   	public CommonResult<Void> deleteMarkLibById(Long  id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   			
   			markLibService.deleteMarkLibById(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("deleteMarkLib发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	
   	/**
   	 * @function 更新标签库
   	 * @param markLibUpdateRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-25 14:58:40
   	*/
   	@ApiOperation("更新标签库")
   	@PostMapping("/updateMarkLib")
   	@OperateTrace(operateType = "修改" , businessType = "标签库" , businessNoKeyPath = "id" ,  titleKeyPath = "libName" )
   	public  CommonResult<Void> updateMarkLib(@Valid @RequestBody MarkLibUpdateRequest markLibUpdateRequest){
   		
   		try {
   			
   			markLibService.updateMarkLib(markLibUpdateRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("updateMarkLib 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 根据ID获取对象
   	 * @param id
   	 * @author 肖荣辉
   	 * @date 2023-12-25 14:58:40
   	*/
   	@ApiOperation("获取标签库信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/findMarkLibById")
   	public CommonResult<MarkLibDTO> findMarkLibById(Long id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   				
   			MarkLibDTO dto =  markLibService.findMarkLibById(id);
   			
   			return CommonResult.success(dto);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findMarkLibById 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   		
   	}
   	
   	/**
   	 * @function 查询标签库列表
   	 * @author 肖荣辉
   	 * @param markLibQueryRequest
   	 * @date 2023-12-25 14:58:40
   	*/
   	@ApiOperation("获取标签库列表")
   	@PostMapping("/findMarkLibList")
   	public CommonResult<CommonPage<MarkLibDTO>> findMarkLibList(@RequestBody  MarkLibQueryRequest markLibQueryRequest){
   		
   		try {
   			 
   			return markLibService.findMarkLibList(markLibQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findMarkLibList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}

}
